package com.yulongtian.algorithms.binarysearch01;

/**
 * https://leetcode.cn/problems/arranging-coins/
 * 可
 *
 * @author yulongTian
 * @create 2023-02-05 17:00
 */
public class Test06 {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(arrangeCoins(2147483647));
    }

    //二分查找
    public static int arrangeCoins(int n) {
        int left = 1;
        int right = n;
        while (left < right) {
            int mid = left + (right - left + 1) / 2;
            if ((long) mid * (mid + 1) <= (long) 2 * n) {
                left = mid;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }

//    //数学的方法
//    public static int arrangeCoins(int n) {
//        for (long i = 0; ; i++) {
//            if (i * (i + 1) > 2 * (long) n) {
//                return (int) (i - 1);
//            }
//        }
//    }

}
